package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findallrole() {
        return roleDao.findallrole();
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setRoleAndPermission(roleId,permissionIds);
        this.setRoleAndMenu(roleId,menuIds);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //        mybatis 分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        List<Integer> permissionIdList = roleDao.findPermissionIdsByRoleId(id);
        return permissionIdList;
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        List<Integer> menuIdList = roleDao.findMenuIdsByRoleId(id);
        return menuIdList;
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.edit(role);
        roleDao.deleteRoleAndPermissionAssociation(role.getId());
        roleDao.deleteRoleAndMenuAssociation(role.getId());
        Integer roleId = role.getId();
        this.setRoleAndPermission(roleId,permissionIds);
        this.setRoleAndMenu(roleId,menuIds);
    }

    @Override
    public void del(Integer id) {
        roleDao.deleteRoleAndPermissionAssociation(id);
        roleDao.deleteRoleAndMenuAssociation(id);
        roleDao.del(id);
    }

    //    抽取  角色 与 权限  关系
    public  void  setRoleAndPermission(Integer roleId,Integer[] permissionIds){
        if (permissionIds !=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    //    抽取   角色 与 菜单 关系
    public  void  setRoleAndMenu(Integer roleId,Integer[] menuIds){
        if (menuIds !=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
}
